package nc.impl.hrppl.pjapplication.pjappl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.HashMap;



import dm.jdbc.util.StringUtil;
import nc.bs.dao.BaseDAO;
import nc.bs.hrppl.util.QueryBudgetMakeDetailUtil;
import nc.bs.framework.common.NCLocator;
import nc.itf.hrpbm.sjxm.sjxmh.ISjxmHVOService;
import nc.itf.hrppl.pjapplication.IUapbdFourCGProjectService;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplHVOService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.ct.uitl.ArrayUtil;
import nc.vo.hrpbm.budgetadjust.AggBudgetAdjustVO;
import nc.vo.hrpbm.budgetadjust.BudgetAdjustDetailVO;
import nc.vo.hrpbm.budgetadjust.BudgetAdjustVO;
import nc.vo.hrpbm.budgetreply.AggBudgetReplyVO;
import nc.vo.hrpbm.budgetreply.BudgetReplyDetailVO;
import nc.vo.hrpbm.budgetreply.BudgetReplyVO;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nc.vo.hrppl.pjapplication.PJApplHVO;
import nc.vo.hrppl.pjapplication.PjapplPVO;
import nc.vo.hrppl.proupdate.AggProUpdateHVO;
import nc.vo.hrppl.proupdate.CGUpdateCVO;
import nc.vo.hrppl.proupdate.ProUpdateHVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.pub.lang.UFDouble;
import java.util.Collections;

public class UapbdFourCGProjectServiceImpl implements IUapbdFourCGProjectService{
    private BaseDAO baseDAO;
    private ISjxmHVOService sjxmHVOService;

    @SuppressWarnings("unchecked")
    @Override
    public void commitWriteBackFourProjectYSAndZZMonry(AggBudgetReplyVO[] vos) throws BusinessException {
        if(vos != null) {
            for(AggBudgetReplyVO aggvo : vos) {
                BudgetReplyVO hvo = aggvo.getParentVO();
                if(hvo.getTaskstate()  != 13 && hvo.getTaskstage() != 2) {continue;}
                String pk_budgetreply = hvo.getPk_budgetreply();
                List<Map<String,Object>> listmaps = (List<Map<String,Object>>)this.getBaseDAO().executeQuery(this.buildPFItemSQL(pk_budgetreply), new MapListProcessor());
                if(listmaps != null && !listmaps.isEmpty()) {
                    for(Map<String,Object> map : listmaps ) {
                        Object pk_project = map.get("pk_project");
                        if(pk_project != null) {
                            String sql = this.buildProjectSQL(pk_project);
                            List<String> lists = (List<String>)this.getBaseDAO().executeQuery(sql, new ColumnListProcessor());
                            if(!lists.isEmpty()) {
                                String s = " update hrpbm_fourcgxm set " +
                                        "   ,budstate = 1 " +
                                        " where pk_fourcgxm in ('" + StringUtil.join(lists.toArray(), "','") + "')";
                                this.getBaseDAO().executeUpdate(s);
                            }
                        }
                    }
                }

            }
        }
    }

    /**
     * 查询预算批复
     * @return
     * @throws BusinessException
     */
    protected String buildPFItemSQL(Object pk_budgetreply) throws BusinessException{
        String sql = " select pk_project,amount_seconddown " +
                "   from hrpbm_budgetreplydetail " +
                "  where pk_budgetreply in ('" + pk_budgetreply + "') ";
        return sql;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void uncommitWriteBackFourProjectYSAndZZMonry(AggBudgetReplyVO[] vos) throws BusinessException {
        if(vos != null) {
            for(AggBudgetReplyVO aggvo : vos) {
                BudgetReplyVO hvo = aggvo.getParentVO();
                if(hvo.getTaskstate()  != 13 && hvo.getTaskstage() != 2) {continue;}
                String pk_budgetreply = hvo.getPk_budgetreply();
                List<Map<String,Object>> listmaps = (List<Map<String,Object>>)this.getBaseDAO().executeQuery(this.buildPFItemSQL(pk_budgetreply), new MapListProcessor());
                if(listmaps != null && !listmaps.isEmpty()) {
                    for(Map<String,Object> map : listmaps ) {
                        Object pk_project = map.get("pk_project");
                        if(pk_project != null) {
                            String sql = this.buildProjectMonerySQL(pk_project);
                            List<String> lists = (List<String>)this.getBaseDAO().executeQuery(sql, new ColumnListProcessor());
                            if(!lists.isEmpty()) {
                                String s = " update hrpbm_fourcgxm set " +
                                        "   ,budstate = 3 " +
                                        " where pk_fourcgxm in ('" + StringUtil.join(lists.toArray(), "','") + "')";
                                this.getBaseDAO().executeUpdate(s);
                            }
                        }

                    }

                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void callbackAPPROVEWriteBackFourProjectTZAndZZMonry(AggBudgetAdjustVO[] vos) throws BusinessException {
        if(vos != null) {
            for(AggBudgetAdjustVO aggvo : vos) {
                BudgetAdjustVO hvo = aggvo.getParentVO();
                String pk_budgetadjust = hvo.getPk_budgetadjust();
                List<Map<String,Object>> listmaps = (List<Map<String,Object>>)this.getBaseDAO().executeQuery(this.builYSTZSQL(pk_budgetadjust),  new MapListProcessor());
                if(listmaps != null && !listmaps.isEmpty()) {
                    for(Map<String,Object> map : listmaps ) {
                        Object pk_project = map.get("pk_project");
                        if(pk_project != null) {
                            String sql = this.buildProjectMonerySQL(pk_project);
                            List<String> lists = (List<String>)this.getBaseDAO().executeQuery(sql, new ColumnListProcessor());
                            if(!lists.isEmpty()) {
                                String s = " update hrpbm_fourcgxm set " +
                                        "   ,fourstate =  2 " +
                                        " where pk_fourcgxm in ('" + StringUtil.join(lists.toArray(), "','") + "')";
                                this.getBaseDAO().executeUpdate(s);
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void closinitemFourCGProject(AggPjapplVO aggvo) throws BusinessException {
        ISuperVO[] pvos = aggvo.getChildren(PjapplPVO.class);
        if(pvos != null) {
            List<Object> lpks = new ArrayList<>();
            for(ISuperVO vo : pvos) {
                Object vbdef1 = vo.getAttributeValue("vbdef1");
                lpks.add(vbdef1);
            }
            if(lpks != null && !lpks.isEmpty()) {
                String sql = this.BuildClosinitemSQL(lpks.toArray());
                this.getBaseDAO().executeUpdate(sql);

            }
        }
    }

    /**
     * 构建调整查询sql
     * @return
     * @throws BusinessException
     */
    protected String builYSTZSQL(Object pk_budgetadjust) throws BusinessException {
        String sql = " select  " +
                "    pk_project,amount_adjust,amount_after_adjust " +
                " from hrpbm_budgetadjustdetail  " +
                " where dr = 0 " +
                "    pk_budgetadjust in ('" + pk_budgetadjust + "')";
        return sql;
    }

    @Override
    public void UnClosinitemFourCGProject(AggPjapplVO aggvo) throws BusinessException {
        ISuperVO[] pvos = aggvo.getChildren(PjapplPVO.class);
        if(pvos != null) {
            List<Object> lpks = new ArrayList<>();
            for(ISuperVO vo : pvos) {
                Object vbdef1 = vo.getAttributeValue("vbdef1");
                lpks.add(vbdef1);
            }
            if(lpks != null && !lpks.isEmpty()) {
                String sql = this.BuildUnClosinitemSQL(lpks.toArray());
                this.getBaseDAO().executeUpdate(sql);

            }
        }
    }

    @Override
    public void updateApproveFourCGProjectCodeAndNameAndMonery(AggPjapplVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        for(AggPjapplVO vo : vos) {
            ArrayList<String> listsqls = new ArrayList<>();
            ISuperVO[] cgsups = vo.getChildren(PjapplPVO.class);
            PJApplHVO hvo = vo.getParentVO();
            Object belongpro = hvo.getAttributeValue("belongpro");// 父项目
            Object def1 = hvo.getAttributeValue("def1");
            Object year = hvo.getAttributeValue("applyyear");
            if(belongpro == null)  continue;
            if(def1 == null) continue;
            if(Integer.valueOf(def1.toString()) != 3) continue;
            if(ArrayUtil.isEmpty(cgsups)) continue;
            Map<String, UFDouble> datas = QueryBudgetMakeDetailUtil.getBudgetMakeDetailData(belongpro);
            UFDouble tzdata = datas.get("tzdata");
            UFDouble zzdata = datas.get("zzdata");
            HashMap<String, Object> param = this.getParentProjectAlls(belongpro,vo);
            if(param.isEmpty()) continue;
            for(ISuperVO cgsup : cgsups ) {
                PjapplPVO cgvo = (PjapplPVO)cgsup;
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                param.put("pk_fourcgxm", pk_fourcgxm);
                param.put("year", year);
                param.put("threeproname", hvo.getBillno());
                param.put("threeprono", hvo.getPjname());
                param.put("threeprotzmoney", tzdata);
                param.put("kymoney", zzdata);
                param.put("threeprofinysmoney", zzdata);
                param.put("threeproysmoney", zzdata);
                String updatesql = this.buildApproveFourCGProject(param);
                listsqls.add(updatesql);
            }
            if(listsqls.isEmpty()) continue;
            for(String upsql : listsqls) {
                this.getBaseDAO().executeUpdate(upsql);
            }
        }
    }

    @Override
    public void revisionOfProjectAdmissionFourCGProjectCodeAndNameAndMonery(AggPjapplVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        //	IPJApplHVOService service = ServiceLocator.find(IPJApplHVOService.class);
        IPJApplHVOService service =NCLocator.getInstance().lookup(IPJApplHVOService.class);
        for(AggPjapplVO vo : vos) {
            ArrayList<String> listsqls = new ArrayList<>();
            //新项目
            ISuperVO[] cgsups = vo.getChildren(PjapplPVO.class);
            PJApplHVO hvo = vo.getParentVO();
            Object belongpro = hvo.getAttributeValue("belongpro");// 父项目
            Object def1 = hvo.getAttributeValue("def1");
            Object totalamount = hvo.getAttributeValue("totalamount");// 最终预算金额
            if(belongpro == null)  continue;
            if(def1 == null) continue;
            if(Integer.valueOf(def1.toString()) != 3) continue;
            if(ArrayUtil.isEmpty(cgsups)) continue;
            HashMap<String, Object> param = this.getParentProjectAlls(belongpro,vo);
            if(param.isEmpty()) continue;
            List<PjapplPVO> pjapplPVOList = Arrays.stream(cgsups)
                    .filter(obj -> obj instanceof PjapplPVO)
                    .map(obj -> (PjapplPVO) obj)
                    .collect(Collectors.toList());

// 使用 Map 来分组，其中键是状态（1、2、3），值是对应的 PjapplPVO 列表
            Map<Integer, List<PjapplPVO>> groupedByStatus = pjapplPVOList.stream()
                    .collect(Collectors.groupingBy(PjapplPVO::getStatus));

// 从分组后的 Map 中提取出修改、新增、删除三组
            List<PjapplPVO> modifiedList = groupedByStatus.getOrDefault(1, Collections.emptyList());
            List<PjapplPVO> addedList = groupedByStatus.getOrDefault(2, Collections.emptyList());
            List<PjapplPVO> removedList = groupedByStatus.getOrDefault(3, Collections.emptyList());
//共有类进行回写
            for(ISuperVO modList : modifiedList ) {
                PjapplPVO cgvo = (PjapplPVO)modList;
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                param.put("pk_fourcgxm", pk_fourcgxm);
                param.put("threeproname", hvo.getBillno());
                //	param.put("budstate", 1);
                param.put("threeprono", hvo.getPjname());
                param.put("threeprotzmoney", new UFDouble(0));
                param.put("kymoney", hvo.getAttributeValue("pjtotalcost"));
                param.put("threeprofinysmoney", hvo.getAttributeValue("pjtotalcost"));
                param.put("threeproysmoney", hvo.getAttributeValue("pjtotalcost"));
                String updatesql = this.buildApproveFourCGProject(param);
                listsqls.add(updatesql);
            }
//新增的类初始态变成打包态
            for(ISuperVO addList : addedList ) {
                PjapplPVO cgvo = (PjapplPVO)addList;
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                param.put("pk_fourcgxm", pk_fourcgxm);
                param.put("threeproname", hvo.getBillno());
                param.put("fourstate", 2);
                param.put("threeprono", hvo.getPjname());
                param.put("threeprotzmoney", new UFDouble(0));
                param.put("kymoney", hvo.getAttributeValue("pjtotalcost"));
                param.put("threeprofinysmoney", hvo.getAttributeValue("pjtotalcost"));
                param.put("threeproysmoney", hvo.getAttributeValue("pjtotalcost"));
                String updatesql = this.buildApproveFourCGProject(param);
                listsqls.add(updatesql);
            }
//被删除的变回初始态并清空对应数据
            for(ISuperVO rList : removedList ) {
                PjapplPVO cgvo = (PjapplPVO)rList;
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                param.put("pk_fourcgxm", pk_fourcgxm);
                //	param.put("threeproname", hvo.getBillno());
                param.put("fourstate", 1);
                param.put("threeprono", hvo.getPjname());
                param.put("threeprotzmoney", new UFDouble(0));
                param.put("kymoney", null);
                param.put("threeprofinysmoney", null);
                param.put("threeproysmoney", null);
                String updatesql = this.buildUNApproveFourCGProject(param);
                listsqls.add(updatesql);
            }

            if(listsqls.isEmpty()) continue;
            for(String upsql : listsqls) {
                this.getBaseDAO().executeUpdate(upsql);
            }
        }
    }

    @Override
    public void updateApproveTZFourCGProjectCodeAndNameAndMonery(AggProUpdateHVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        for(AggProUpdateHVO vo : vos) {
            ArrayList<String> listsqls = new ArrayList<>();
            ISuperVO[] cgsups = vo.getChildren(CGUpdateCVO.class);
            ProUpdateHVO hvo = vo.getParentVO();
            Object belongpro = hvo.getAttributeValue("pk_proapp");// 父项目
            if(ArrayUtil.isEmpty(cgsups)) continue;
            HashMap<String, Object> param = this.getTZParentProjectAlls(belongpro, vo);
            Map<String, UFDouble> data = QueryBudgetMakeDetailUtil.getBudgetMakeDetailData(belongpro);
            UFDouble tzmonery = data.get("tzmonery");
            UFDouble zzmonery = data.get("zzmonery");
            if(param.isEmpty()) continue;
            //项目审批后 项目调整单 被删除的单据 //状态回写未初始化，未被删除的执行调整
            //vbdef1为对应的4级项目
            for(ISuperVO cgsup : cgsups ) {
                CGUpdateCVO cgvo = (CGUpdateCVO)cgsup;
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                param.put("pk_fourcgxm", pk_fourcgxm);
                param.put("threeprotzmoney", tzmonery);
                param.put("kymoney", zzmonery);
                param.put("budstate", 3);
                param.put("threeprofinysmoney", zzmonery);
                param.put("threeproysmoney", zzmonery);
                String updatesql = this.buildApproveFourCGProject(param);
                listsqls.add(updatesql);
            }
            if(listsqls.isEmpty()) continue;
            for(String upsql : listsqls) {
                this.getBaseDAO().executeUpdate(upsql);
            }
        }

    }

    @Override
    public void approveUpdateFourCGProjectYSMonery(AggProUpdateHVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        for(AggProUpdateHVO vo : vos) {
            ProUpdateHVO hvo = vo.getParentVO();
            Object pk_proapp = hvo.getAttributeValue("pk_proapp");// // 所调整的项目
            ArrayList<String> listsqls = new ArrayList<>();
            ISuperVO[] cgsups = vo.getChildren(CGUpdateCVO.class);
            if(ArrayUtil.isEmpty(cgsups)) continue;
            for(ISuperVO cgvo : cgsups) {
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                StringBuilder sql = new StringBuilder();
                sql.append(" update hrpbm_fourcgxm set ");
                String time = new UFDateTime().toString();
                sql.append(" ts = '"+time+"',lastmaketime= '"+time+"',modifiedtime ='"+time+"',modifyobj = 'HOP', fourstate = 2 ");
                sql.append(" where pk_fourcgxm = '"+pk_fourcgxm+"' ");
                listsqls.add(sql.toString());
            }
            if(listsqls.isEmpty()) continue;
            for(String sql : listsqls) {
                this.getBaseDAO().executeUpdate(sql);
            }
        }
    }

    @Override
    public void UnApproveUpdateFourCGProjectYSMonery(AggProUpdateHVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        for(AggProUpdateHVO vo : vos) {
            ArrayList<String> listsqls = new ArrayList<>();
            ISuperVO[] cgsups = vo.getChildren(CGUpdateCVO.class);
            if(ArrayUtil.isEmpty(cgsups)) continue;
            for(ISuperVO cgvo : cgsups) {
                Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_fourcgxm == null) continue;
                StringBuilder sql = new StringBuilder();
                sql.append(" update hrpbm_fourcgxm set ");
                String time = new UFDateTime().toString();
                sql.append(" ts = '"+time+"',lastmaketime= '"+time+"',modifiedtime ='"+time+"',modifyobj = 'HOP', fourstate = 1 ");
                sql.append(",budstate = 1 ,oneprono = null,oneproname= null, twoprono=null,twoproname= null, " );
                sql.append(" kymoney = null, threeprofinysmoney= null, threeprotzmoney= null, threeproysmoney=null, ");
                sql.append(" threeprono=null,threeproname=null ");
                sql.append(" where pk_fourcgxm = '"+pk_fourcgxm+"' ");
                listsqls.add(sql.toString());
            }
            if(listsqls.isEmpty()) continue;
            for(String sql : listsqls) {
                this.getBaseDAO().executeUpdate(sql);
            }
        }
    }

    @Override
    public void updateUnApproveFourCGProjectCodeAndNameAndMonery(AggPjapplVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        for(AggPjapplVO vo : vos) {
            ISuperVO[] cgsups = vo.getChildren(PjapplPVO.class);
            PJApplHVO hvo = vo.getParentVO();
            Object belongpro = hvo.getAttributeValue("belongpro");
            Object def1 = hvo.getAttributeValue("def1");
            if(belongpro == null)  continue;
            if(def1 == null) continue;
            if(Integer.valueOf(def1.toString()) != 3) continue;
            if(ArrayUtil.isEmpty(cgsups)) continue;
            ArrayList<String> listsqls = new ArrayList<>();
            HashMap<String, Object> param = this.getParentProjectAlls(belongpro,vo);
            if(param.isEmpty()) continue;
            for(ISuperVO cgsup : cgsups ) {
                PjapplPVO cgvo = (PjapplPVO)cgsup;
                Object pk_cgxm = cgvo.getAttributeValue("vbdef1");
                if(pk_cgxm == null) continue;
                param.put("pk_fourcgxm", cgvo.getAttributeValue("vbdef1"));
                String sql = this.buildUNApproveFourCGProject(param);
                listsqls.add(sql);
            }
            if(listsqls.isEmpty()) continue;
            for(String sql : listsqls) {
                this.getBaseDAO().executeUpdate(sql);
            }
        }

    }

    /**
     * 查询项目申请单中的采购项目
     * @param pk_pjappl
     * @return
     * @throws BusinessException
     */
    protected String buildProjectSQL(Object pk_pjappl) throws BusinessException{
        String sql = " select  " +
                "   distinct p.vbdef1  " +
                " from hrppl_pjappl h " +
                " left join hrppl_pjappl_p p on h.pk_pjappl = p.pk_pjappl " +
                " where h.dr = 0 and p.dr = 0 " +
                " and h.pk_pjappl = '" + pk_pjappl + "'";
        return sql;
    }

    /**
     * 获取项目申请单金额
     * @param pk_pjappl
     * @return
     * @throws BusinessException
     */
    protected String buildProjectMonerySQL(Object pk_pjappl) throws BusinessException{
        String sql = " select  " +
                "    h.pjtotalcost  " +
                " from hrppl_pjappl h " +
                " left join hrppl_pjappl_p p on h.pk_pjappl = p.pk_pjappl " +
                " where h.dr = 0 and p.dr = 0 " +
                " and h.pk_pjappl = '" + pk_pjappl + "'";
        return sql;
    }

    /**
     * 项目调整
     * 获取当前项目所以上级
     * @param belongpro
     * @return
     * @throws BusinessException
     */
    @SuppressWarnings("unchecked")
    protected HashMap<String, Object> getTZParentProjectAlls(Object belongpro,AggProUpdateHVO vo) throws  BusinessException{
        HashMap<String, Object> param = new HashMap<String,Object>();
        Map<String, Object> o = (Map<String, Object>)this.getBaseDAO().executeQuery("select billno, pjname from hrppl_pjappl where dr = 0 and pk_pjappl = '"+belongpro+"' ", new MapProcessor());
        param.put("threeprono", o.get("billno"));
        param.put("threeproname", o.get("pjname"));
        String sql = "select def1 from bd_project where dr = 0 " +
                " and pk_project ='" + belongpro + "' ";
        Object obj = this.getBaseDAO().executeQuery(sql, new ColumnProcessor());
        if(obj == null ) return param;
        Integer i = Integer.valueOf(obj.toString());
        Object pk_project = belongpro;
        while(i > 0 ) {
            String isql = "select project_code,project_name,pk_parentpro from bd_project where dr = 0 " +
                    " and pk_project ='" + pk_project + "' ";
            Object iobj = this.getBaseDAO().executeQuery(isql, new MapProcessor());
            if(iobj ==  null) {
                i--;
                continue;
            }
            Map<String,Object> map  =  (Map<String,Object>)iobj;
            Object project_code = map.get("project_code");
            Object project_name = map.get("project_name");
            pk_project = map.get("pk_parentpro");
            if(i == 3) {
                param.put("threeprono", project_code);
                param.put("threeproname", project_name);

            }else if(i == 2) {
                param.put("twoprono", project_code);
                param.put("twoproname", project_name);

            }else if(i == 1) {
                param.put("oneprono", project_code);
                param.put("oneproname", project_name);
            }
            i--;
        }
        return  param;
    }
    /**
     * 项目申请
     * 获取当前项目所以上级
     * @param belongpro
     * @return
     * @throws BusinessException
     */
    @SuppressWarnings("unchecked")
    protected HashMap<String, Object> getParentProjectAlls(Object belongpro,AggPjapplVO vo) throws  BusinessException{
        HashMap<String, Object> param = new HashMap<String,Object>();
        PJApplHVO hvo = vo.getParentVO();
        param.put("threeprono", hvo.getAttributeValue("billno"));
        param.put("threeproname", hvo.getAttributeValue("pjname"));
        String sql = "select def1 from bd_project where dr = 0 " +
                " and pk_project ='" + belongpro + "' ";
        Object obj = this.getBaseDAO().executeQuery(sql, new ColumnProcessor());
        if(obj == null ) return param;
        Integer i = Integer.valueOf(obj.toString());
        Object pk_project = belongpro;
        while(i > 0 ) {
            String isql = "select project_code,project_name,pk_parentpro from bd_project where dr = 0 " +
                    " and pk_project ='" + pk_project + "' ";
            Object iobj = this.getBaseDAO().executeQuery(isql, new MapProcessor());
            if(iobj ==  null) {
                i--;
                continue;
            }
            Map<String,Object> map  =  (Map<String,Object>)iobj;
            Object project_code = map.get("project_code");
            Object project_name = map.get("project_name");
            pk_project = map.get("pk_parentpro");
            if(i == 3) {
                param.put("threeprono", project_code);
                param.put("threeproname", project_name);

            }else if(i == 2) {
                param.put("twoprono", project_code);
                param.put("twoproname", project_name);

            }else if(i == 1) {
                param.put("oneprono", project_code);
                param.put("oneproname", project_name);
            }
            i--;
        }
        return  param;
    }

    /**
     * 构建取消结项sql
     * @return
     * @throws BusinessException
     */
    protected String BuildUnClosinitemSQL(Object[] pks) throws BusinessException{
        String sql = "update hrpbm_fourcgxm set fourstate = 2 where " +
                " pk_fourcgxm in " +
                "('" + StringUtil.join(pks, "','") + "')";
        return sql;
    }
    /**
     * 构建结项sql
     * @return
     * @throws BusinessException
     */
    protected String BuildClosinitemSQL(Object[] pks) throws BusinessException{
        String sql = "update hrpbm_fourcgxm set fourstate = 5 where " +
                " pk_fourcgxm in " +
                "('" + StringUtil.join(pks, "','") + "')";
        return sql;
    }
    /**
     * 构建审批通过的sql
     * @param upParam
     * @return
     * @throws BusinessException
     */
    protected String buildApproveFourCGProject(Map<String,Object> upParam) throws BusinessException{
        StringBuilder sql = new StringBuilder();
        sql.append(" update hrpbm_fourcgxm set ");
        String time = new UFDateTime().toString();
        sql.append(" ts = '"+time+"',lastmaketime= '"+time+"',modifiedtime ='"+time+"',modifyobj = 'HOP', fourstate = 2 , budstate= 2 ");
        if (upParam.get("threeprotzmoney") !=  null) sql.append(", threeprotzmoney = " + upParam.get("threeprotzmoney"));
        if (upParam.get("kymoney") !=  null) sql.append(" , kymoney = '" + upParam.get("kymoney")+"'");
        if (upParam.get("threeprofinysmoney") !=  null) sql.append(" , threeprofinysmoney = " + upParam.get("threeprofinysmoney"));
        if (upParam.get("threeproysmoney") !=  null) sql.append(" , threeproysmoney = " + upParam.get("threeproysmoney"));
        if (upParam.get("threeprono") !=  null) sql.append(" , threeprono = '" + upParam.get("threeprono")+ "'");
        if (upParam.get("threeproname") !=  null) sql.append(" , threeproname = '" + upParam.get("threeproname")+ "'");
        if (upParam.get("twoprono") !=  null) sql.append(" , twoprono = '" + upParam.get("twoprono")+ "'");
        if (upParam.get("twoproname") !=  null) sql.append(" , twoproname = '" + upParam.get("twoproname")+ "'");
        if (upParam.get("oneprono") !=  null) sql.append(" , oneprono = '" + upParam.get("oneprono")+ "'");
        if (upParam.get("oneproname") !=  null) sql.append(" , oneproname = '" + upParam.get("oneproname")+ "'");
        sql.append(" where pk_fourcgxm = '"+upParam.get("pk_fourcgxm")+"' ");
        return sql.toString();
    }
    /**
     * 构建收回的修改sql
     * @param upParam
     * @return
     * @throws BusinessException
     */
    protected String buildUNApproveFourCGProject(Map<String,Object> upParam) throws BusinessException{
        StringBuilder sql = new StringBuilder();
//		update hrpbm_fourcgxm set oneprono ='onecode',oneproname='onename',twoprono = '',twoproname='',
//				threeprono='',threeproname='',threeprofinysmoney= 0,threeprotzmoney= 0 ,threeproysmoney= 0 ,
//				fourprono = '',fourproname='' where pk_fourcgxm = '1001ZZ10000000040ZWW'
        sql.append(" update hrpbm_fourcgxm set ");
        String time = new UFDateTime().toString();
        sql.append(" ts = '"+time+"', lastmaketime= '"+time+"', modifiedtime ='"+time+"', modifyobj = 'HOP',fourstate = 1 ");
        sql.append(", oneprono = null ");
        sql.append(", oneproname= null ");
        sql.append(",twoprono = null ");
        sql.append(",twoproname = null ");
        sql.append(",budstate = 2 ");
        sql.append(",threeprono = null ");
        sql.append(",threeproname = null ");
        // 最终调整金额
        sql.append(",threeprofinysmoney = null ");
        //调整金额
        sql.append(",threeprotzmoney = null ");
        // 预算金额
        sql.append(",threeproysmoney = null ");
        sql.append(" where pk_fourcgxm = '"+upParam.get("pk_fourcgxm")+"' ");
        return sql.toString();
    }

    public BaseDAO getBaseDAO() {
        return baseDAO == null ? new BaseDAO() : baseDAO;
    }
}
